package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.WarehouseDao;
import com.woniuxy.entity.Production;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Warehouse;
import com.woniuxy.service.WarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Resource
    WarehouseDao warehouseDao;

    @Override
    public ResponseEntity findAll() {

        return new ResponseEntity<>(200, "成功", warehouseDao.findAll());
    }

    @Override
    public ResponseEntity findOne(String id) {
        Warehouse warehouse = warehouseDao.findOne(id);
        return new ResponseEntity(200, "成功", warehouse);
    }

    @Override
    public ResponseEntity addOne(Warehouse warehouse) {
        int index = warehouseDao.addOne(warehouse);
        if (index > 0) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity updateOne(Warehouse warehouse) {
        int index = warehouseDao.updateOne(warehouse);
        if (index > 0) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteOne(String id) {
        int index = warehouseDao.deleteOne(id);
        if (index > 0) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Warehouse> list=warehouseDao.findAll();
        PageInfo<Warehouse> pageInfo=PageInfo.of(list);
        return new ResponseEntity(200, "成功", pageInfo);
    }

    @Override
    public ResponseEntity deleteSome(int[] id) {
        int index = warehouseDao.deleteSome(id);
        if (index > 0) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByCode(String warehouse_id) {
        Warehouse warehouse = warehouseDao.findByCode(warehouse_id);
        if (warehouse!=null){
            return new ResponseEntity(200,"成功",warehouse);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByWname(String name) {
        Warehouse warehouse = warehouseDao.findByWname(name);
        if (warehouse!=null){
            return new ResponseEntity(200,"成功",warehouse);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity importExcel(List<Warehouse> list) {
        int index = warehouseDao.addBatch(list);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }


    public ResponseEntity findByName(int currentPage,int pageSize,String name){
        PageHelper.startPage(currentPage,pageSize);
        List<Warehouse> list=warehouseDao.findByName(name);
        PageInfo<Warehouse> pageInfo=PageInfo.of(list);
        return new ResponseEntity(200, "成功", list);
    }
}
